package com.easy.query.solon.web.controller;

import com.easy.query.api4j.client.EasyQuery;
import com.easy.query.core.annotation.EasyQueryTrack;
import com.easy.query.solon.annotation.Db;
import com.easy.query.solon.web.domain.TestUpdateEntity;
import com.easy.query.solon.web.domain.TestUpdateVersionEntity;
import com.easy.query.solon.web.dto.TestUpdate2Rquest;
import org.dromara.hutool.core.bean.BeanUtil;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.validation.annotation.Validated;

import java.util.UUID;

/**
 * create time 2023/8/21 21:57
 * 文件说明
 *
 * @author xuejiaming
 */
@Controller
@Mapping("/testUpdateVersion")
public class TestUpdateVersionController {

    @Db
    private EasyQuery easyQuery;
    @Mapping(value = "/init",method = MethodType.GET)
    public String testUpdate1(){
        TestUpdateVersionEntity testUpdateVersionEntity = new TestUpdateVersionEntity();
        testUpdateVersionEntity.setId("1");
        testUpdateVersionEntity.setName("测试1");
        testUpdateVersionEntity.setStatus(0);
        testUpdateVersionEntity.setDescription("描述信息");
        testUpdateVersionEntity.setVersion(UUID.randomUUID().toString().replaceAll("-",""));
        easyQuery.insertable(testUpdateVersionEntity).executeRows();
        return "ok";
    }

    @Mapping(value = "/testUpdate2",method = MethodType.POST)
    public String testUpdate2(@Validated TestUpdate2Rquest request){
        TestUpdateVersionEntity testUpdateVersionEntity = easyQuery.queryable(TestUpdateVersionEntity.class)
                .whereById(request.getId()).firstNotNull("未找到对应的记录");
        if(!testUpdateVersionEntity.getStatus().equals(0)){
            return "当前状态不是0";
        }
        BeanUtil.copyProperties(request,testUpdateVersionEntity);
        testUpdateVersionEntity.setStatus(1);
        easyQuery.updatable(testUpdateVersionEntity).executeRows();
        return "ok";
    }
    @EasyQueryTrack
    @Mapping(value = "/testUpdate3",method = MethodType.POST)
    public String testUpdate3(@Validated TestUpdate2Rquest request){
        TestUpdateVersionEntity testUpdateVersionEntity = easyQuery.queryable(TestUpdateVersionEntity.class)
                .whereById(request.getId()).firstNotNull("未找到对应的记录");
        if(!testUpdateVersionEntity.getStatus().equals(0)){
            return "当前状态不是0";
        }
        BeanUtil.copyProperties(request,testUpdateVersionEntity);
        testUpdateVersionEntity.setStatus(1);
        easyQuery.updatable(testUpdateVersionEntity).executeRows();
        return "ok";
    }
}
